[My Github link] https://github.com/sydneydlu98/DSI_Data_Challenge_5
In this Data Challenge, we will be clustering foods from the nndb_flat dataset provided on Canvas. To load/clean the data as well as perform some exploratory analysis:
## load all the packages
library(readr)
library(dplyr)
library(GGally)
library(tidyverse)
library(plotly)
library(ggplot2)
## read in data
data <- read_csv("nndb_flat.csv")
## filter the data to only contain food groups of Vegetables and Vegetable Products, Beef Products, and Sweets
object <-
c("Sweets", "Beef Products", "Vegetables and Vegetable Products")
clean_data <- data %>%
subset(FoodGroup %in% object)
var <- clean_data %>%
select(Energy_kcal:Zinc_mg)
GGally::ggcorr(var)
Steps for performing the PCA on the data:
data_scaled <- scale(var)
pca_data <- prcomp(data_scaled,
center = FALSE,
scale. = FALSE)
summary(pca_data)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 2.3024 1.8455 1.6577 1.6205 1.41770 1.03167 0.9922
## Proportion of Variance 0.2305 0.1481 0.1195 0.1142 0.08739 0.04628 0.0428
## Cumulative Proportion 0.2305 0.3785 0.4980 0.6122 0.69960 0.74587 0.7887
## PC8 PC9 PC10 PC11 PC12 PC13 PC14
## Standard deviation 0.90473 0.85456 0.8279 0.74550 0.71398 0.61241 0.58307
## Proportion of Variance 0.03559 0.03175 0.0298 0.02416 0.02216 0.01631 0.01478
## Cumulative Proportion 0.82426 0.85602 0.8858 0.90998 0.93214 0.94845 0.96323
## PC15 PC16 PC17 PC18 PC19 PC20 PC21
## Standard deviation 0.46385 0.42317 0.38016 0.31599 0.27229 0.23596 0.22717
## Proportion of Variance 0.00935 0.00779 0.00628 0.00434 0.00322 0.00242 0.00224
## Cumulative Proportion 0.97258 0.98037 0.98665 0.99099 0.99422 0.99664 0.99888
## PC22 PC23
## Standard deviation 0.1440 0.07045
## Proportion of Variance 0.0009 0.00022
## Cumulative Proportion 0.9998 1.00000
var_explainded <- summary(pca_data)$importance[2, ]
cumulative <- cumsum(var_explainded)
var_explained_df <- data.frame(PC = 1:23,
cum_var_explained = cumulative)
var_explained_df
var_explained_df %>%
ggplot(aes(x = PC, y = cum_var_explained, group = 1)) +
geom_point() +
geom_line(lwd = 1) +
labs(title = "Scree Plot: PCA",
y = 'Cumulative Variation Explained',
x = 'PC') +
ggtitle("Plot of cumulative proportion of the variation explained by each PC") +
theme(plot.title = element_text(hjust = 0.5, size = 16)) +
# add line for better visualization
geom_vline(xintercept = 3,
linetype = 2,
lwd = 1,
col = "red")
pca_loadings <- as.data.frame(pca_data$rotation) %>%
dplyr::select(PC1, PC2, PC3) %>%
mutate(variable = rownames(pca_data$rotation)) %>%
pivot_longer(
cols = c('PC1', 'PC2', 'PC3'),
names_to = 'PC',
values_to = 'loadings'
)
ggplot(pca_loadings, aes(x = variable,
y = loadings)) +
geom_bar(stat = 'identity') +
facet_wrap(~ PC) +
theme(axis.text.x = element_text(
angle = 60,
hjust = 1,
size = 6
)) +
ggtitle("Plots for the loadings for the first 3 PCs for all of the variables") +
theme(plot.title = element_text(hjust = 0.5, size = 16)) +
labs(y = "Loadings", x = "Variables")
pca_scores <- as.data.frame(pca_data$x)
pca_scores <- pca_scores %>%
mutate(FoodGroup = clean_data$FoodGroup) %>%
mutate(description = clean_data$ShortDescrip)
pca_scores
plot1 <- ggplot(pca_scores,
aes(
x = PC1,
y = PC2,
col = FoodGroup,
label = description
)) +
geom_point() +
ggtitle("PC1 versus PC2") +
theme(plot.title = element_text(hjust = 0.5, size = 16))
ggplotly(plot1)
plot2 <- ggplot(pca_scores, aes(x = PC1,
y = PC3,
col = FoodGroup,
label = description)) +
geom_point() +
ggtitle("PC1 versus PC3") +
theme(plot.title = element_text(hjust = 0.5, size = 16))
ggplotly(plot2)
plot3 <- ggplot(pca_scores, aes(x = PC2,
y = PC3,
col = FoodGroup,
label = description)) +
geom_point() +
ggtitle("PC2 versus PC3") +
theme(plot.title = element_text(hjust = 0.5, size = 16))
ggplotly(plot3)
The major outlier on the plots above is yeast extract spread from the food group vegetable and vegatable products.
Then we remove this outlier.
Perform PCA again on the dataset without the outlier (steps 1-4 in the Performing PCA section above) and look at the loadings of the first 3 PCs. Have these changed? Investigate and comment on what could have caused any changes.
Describe what you see in the plots of the scores and interpret this in conjunction with the loadings that you observed for the PCs.